﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BasketballAI.Infrastructure;
using BasketballAI.Infrastructure.GameItems;
using System.Media;
namespace BasketballAI
{
    public partial class BBGame : Form
    {
        #region Members
        
        //Game game;
        Timer timerPlayer11;
        Timer timerPlayer12;
        Timer timerPlayer13;
        Timer timerPlayer21;
        Timer timerPlayer22;
        Timer timerPlayer23;
        Timer timerBall;
        Timer timarUpdateGui;
        Timer timerManage=new Timer();
        #endregion Members

        #region Constructors
        public BBGame()
        {
            InitializeComponent();
            EnalbeDoubelBuffer();




        }
        #endregion Constructors

        #region Methods
        ~BBGame()
        {
            timerPlayer11.Dispose();
            timerPlayer21.Dispose();
            timerPlayer12.Dispose();
            timerPlayer13.Dispose();
            timerPlayer22.Dispose();
            timerPlayer23.Dispose();
            timerBall.Dispose();
            timarUpdateGui.Dispose();
        }
        private void BuildGUI()
        {            
            
            if (Global.Game != null)
            {
                //build gui Player.
                for (int i = 0; i < Global.Game.Teams.Length; i++)
                {
                    for(int j=0;j<Global.Game.Teams[i].TeamPlayers.Length;j++)
                    {
                        System.Windows.Forms.PictureBox pb=new PictureBox();
                        pb.BackColor = System.Drawing.Color.Transparent;
                        pb.Location = Global.Game.ConvertPositionFromMat2Pix(Global.Game.Teams[i].TeamPlayers[j].PositionOnCourt);
                        pb.Name = "pb" + Global.Game.Teams[i].TeamPlayers[j].PlayerName;
                        pb.Size = new System.Drawing.Size(20, 20);                                                
                        pb.Visible =  true;                                                                   
                        pb.BackgroundImage = Global.Game.Teams[i].TeamPlayers[j].Image;
                        ToolTip tt = new ToolTip();
                        tt.SetToolTip(pb, Global.Game.Teams[i].TeamPlayers[j].PlayerName);
                        this.Controls.Add(pb);
                  
                    }
                }        
                //build gui ball.
                System.Windows.Forms.PictureBox pbb = new PictureBox();
                pbb.BackColor = System.Drawing.Color.Transparent;
                pbb.Location = Global.Game.Ball.PositionOnCourtPix;
                pbb.Name = "pbBall";
                pbb.Size = new System.Drawing.Size(20, 20);
                pbb.Visible = true;                
                pbb.BackgroundImage = Properties.Resources.ball;
                //this.tableLayoutPanel1.Controls.Add(pbb, 0, 0);
                this.Controls.Add(pbb);
                //build court.
                System.Windows.Forms.PictureBox Court =new PictureBox();
                Court.Image = Properties.Resources.court;
                
                Court.Location = new System.Drawing.Point(40, 40);
                Court.Name = "Court";
                Court.Size = new System.Drawing.Size(700, 380);                
                Court.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                this.tableLayoutPanel4.Controls.Add(Court, 1, 1);
                this.tableLayoutPanel1.SendToBack();
                //this.Controls.Add(Court);             
                
            }

        }
        private void playBasketSound()
        {                                  
            SoundPlayer simpleSound = new SoundPlayer(Properties.Resources.hitcrowdcheer);                     
            simpleSound.Play();
        }
        private void EnalbeDoubelBuffer()     
        {
                    this.SetStyle(ControlStyles.AllPaintingInWmPaint |
                                  ControlStyles.UserPaint | 
                                  ControlStyles.DoubleBuffer,true);

        }
        #endregion Methods

        #region Init Function

        private void InitTimerUpdateGUI(int interval)
        {            
            timarUpdateGui = new Timer();
            timarUpdateGui.Interval = interval;
            timarUpdateGui.Tick += new EventHandler(UpdateGUI);
            timarUpdateGui.Start();
        }
        private void InitTimerBall(int interval)
        {
            timerBall = new Timer();
            timerBall.Interval = interval;
            timerBall.Tick += new EventHandler(TimerBallTick);           
        }
        private void InitTimerPlayer()
        {
            timerPlayer11 = new Timer();
            timerPlayer11.Interval = Properties.Settings.Default.timerPlayer11Ms;
            timerPlayer11.Tick += new EventHandler(TimerPlayer11Tick);
           
            timerPlayer21 = new Timer();
            timerPlayer21.Interval = Properties.Settings.Default.timerPlayer21Ms;
            timerPlayer21.Tick += new EventHandler(TimerPlayer21Tick);
         
            if (Properties.Settings.Default.NumOfPlayersInTeam > 1)
            {
                timerPlayer12 = new Timer();
                timerPlayer12.Interval = Properties.Settings.Default.timerPlayer12Ms;
                timerPlayer12.Tick += new EventHandler(TimerPlayer12Tick);
               
                timerPlayer13 = new Timer();
                timerPlayer13.Interval = Properties.Settings.Default.timerPlayer13Ms;
                timerPlayer13.Tick += new EventHandler(TimerPlayer13Tick);              

                timerPlayer22 = new Timer();
                timerPlayer22.Interval = Properties.Settings.Default.timerPlayer22Ms;
                timerPlayer22.Tick += new EventHandler(TimerPlayer22Tick);
                
                timerPlayer23 = new Timer();
                timerPlayer23.Interval = Properties.Settings.Default.timerPlayer23Ms;
                timerPlayer23.Tick += new EventHandler(TimerPlayer23Tick);
              
            }
        }
        #endregion Init Function

        #region Events

        private void BBGame_Load(object sender, EventArgs e)
        {          

            Global.Game.StartGame();
            BuildGUI();
            Global.Game.onHitTheBasket += new Game.delegateHitTheBasket(this.UpdateResult);
            Global.Game.Teams[0].TeamPlayers[0].trace += new Player.delegateTrace(WriteTrace2Window);
            Global.Game.Teams[0].TeamPlayers[1].trace += new Player.delegateTrace(WriteTrace2Window);
            Global.Game.Teams[0].TeamPlayers[2].trace += new Player.delegateTrace(WriteTrace2Window);
            Global.Game.Teams[1].TeamPlayers[0].trace += new Player.delegateTrace(WriteTrace2Window);
            Global.Game.Teams[1].TeamPlayers[1].trace += new Player.delegateTrace(WriteTrace2Window);
            Global.Game.Teams[1].TeamPlayers[2].trace += new Player.delegateTrace(WriteTrace2Window);
            InitTimerPlayer();
            InitTimerBall(Properties.Settings.Default.TimerBall);
            InitTimerUpdateGUI(Properties.Settings.Default.TimerGUIMs);
            this.chkP11.Text = Properties.Settings.Default.Team1PlayerNames.Split(',')[0];
            this.chkP12.Text = Properties.Settings.Default.Team1PlayerNames.Split(',')[1];
            this.chkP13.Text = Properties.Settings.Default.Team1PlayerNames.Split(',')[2];
            this.chkP21.Text = Properties.Settings.Default.Team2PlayerNames.Split(',')[0];
            this.chkP22.Text = Properties.Settings.Default.Team2PlayerNames.Split(',')[1];
            this.chkP23.Text = Properties.Settings.Default.Team2PlayerNames.Split(',')[2];

            
        }

        private void UpdateGUI(object o, EventArgs e)
        {           
            foreach (Player p in Global.Game.Teams[0].TeamPlayers)
            {
                PictureBox pb = (PictureBox)(this.Controls.Find("pb" + p.PlayerName, true))[0];
                pb.Location = Global.Game.ConvertPositionFromMat2Pix(p.PositionOnCourt);
                pb.BackgroundImage = p.Image;            

            }
            foreach (Player p in Global.Game.Teams[1].TeamPlayers)
            {
                PictureBox pb = (PictureBox)(this.Controls.Find("pb" + p.PlayerName, true))[0];
                pb.Location = Global.Game.ConvertPositionFromMat2Pix(p.PositionOnCourt);
                pb.BackgroundImage = p.Image;               
            }

            PictureBox pbb = (PictureBox)(this.Controls.Find("pbBall", true))[0];
            pbb.Location = Global.Game.Ball.PositionOnCourtPix;
            

        }
        private void TimerPlayer11Tick(object o, EventArgs e)
        {
            Global.Game.Teams[0].TeamPlayers[0].MakeDecision();
        }
        private void TimerPlayer12Tick(object o, EventArgs e)
        {
            Global.Game.Teams[0].TeamPlayers[1].MakeDecision();
        }
        private void TimerPlayer13Tick(object o, EventArgs e)
        {
            Global.Game.Teams[0].TeamPlayers[2].MakeDecision();
        }
        private void TimerPlayer21Tick(object o, EventArgs e)
        {
            Global.Game.Teams[1].TeamPlayers[0].MakeDecision();
        }
        private void TimerPlayer22Tick(object o, EventArgs e)
        {
            Global.Game.Teams[1].TeamPlayers[1].MakeDecision();
        }
        private void TimerPlayer23Tick(object o, EventArgs e)
        {
            Global.Game.Teams[1].TeamPlayers[2].MakeDecision();
        }
        private void TimerBallTick(object o, EventArgs e)
        {
            if (Global.Game.Ball.DestinationOnCourtPix != Global.Game.Ball.PositionOnCourtPix)
            {
                Global.Game.Ball.Move(Global.BallStrength.Hard);
            }
       
        }


        private void WriteTrace2Window(string str)
        {
            if(chkP11.Checked)
            {
                if (str.IndexOf(chkP11.Text) != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
            }
            else if (chkP12.Checked)
            {
                if (str.IndexOf(chkP12.Text) != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
            }
            else if (chkP13.Checked)
            {
                if (str.IndexOf(chkP13.Text) != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
            }
            else if (chkP21.Checked)
            {
                if (str.IndexOf(chkP21.Text) != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
            }
            else if (chkP22.Checked)
            {
                if (str.IndexOf(chkP22.Text) != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
            }
            else if (chkP23.Checked)
            {
                if (str.IndexOf(chkP23.Text) != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
            }
            else if (str.IndexOf("test") != -1)
                {
                    this.txtTraceWindow.AppendText(str + "\n");
                }
                

        
        }
        private void btnStopTimer_Click(object sender, EventArgs e)
        {
            timerPlayer11.Stop();
            timerPlayer21.Stop();
            timerPlayer12.Stop();
            timerPlayer13.Stop();
            timerPlayer22.Stop();
            timerPlayer23.Stop();
            timerBall.Stop();

        }

        private void btnStartTimer_Click(object sender, EventArgs e)
        {
        
            timerPlayer11.Start();
            timerPlayer21.Start();
            timerPlayer12.Start();
            timerPlayer13.Start();
            timerPlayer22.Start();
            timerPlayer23.Start();
            timerBall.Start();
        }

        private void btnclearTrace_Click(object sender, EventArgs e)
        {
            this.txtTraceWindow.Clear();              
        }

        private void BBGame_FormClosing(object sender, FormClosingEventArgs e)
        {
            timerPlayer11.Stop();
            timerPlayer21.Stop();
            timerPlayer12.Stop();
            timerPlayer13.Stop();
            timerPlayer22.Stop();
            timerPlayer23.Stop();
            timerBall.Stop();
            timarUpdateGui.Stop();
        }
        private void btnBack_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void UpdateResult(BasketballAI.Infrastructure.Entity.IItem basket)
        {
            if (basket == Global.Game.Teams[0].TargetBasket)
            {
                this.lblHomeResult.Text = (Convert.ToInt32(this.lblHomeResult.Text) + 2).ToString();
                Global.Game.Teams[1].TeamPlayers[0].GoAndTakeBallFromBasket();
            }
            else if (basket == Global.Game.Teams[1].TargetBasket)
            {
                this.lblAwayResult.Text = (Convert.ToInt32(this.lblAwayResult.Text) + 2).ToString();
                Global.Game.Teams[0].TeamPlayers[0].GoAndTakeBallFromBasket();
            }
            playBasketSound();
            this.picShoot.Visible = true;
            timerManage.Interval = 5000;
            timerManage.Tick += new EventHandler(HidePicShoot);
            timerManage.Start();
        }

        private void HidePicShoot(object sender, EventArgs e)
        {
            this.picShoot.Visible = false;
            timerManage.Stop();
            timerManage.Tick -= new EventHandler(HidePicShoot); ;
            
        }

        #endregion Events

        
    

   

      









    }
}
